package com.dj.mingkong.portal.biz.service.system.impl;

import com.dj.mingkong.common.constant.CommonConstants;
import com.dj.mingkong.common.constant.RedisConstants;
import com.dj.mingkong.common.enums.StatusEnum;
import com.dj.mingkong.common.infrastructure.eo.database.system.DictionaryEo;
import com.dj.mingkong.common.util.SmartBeanCopy;
import com.dj.mingkong.portal.biz.controller.system.vo.response.DictionaryVo;
import com.dj.mingkong.portal.biz.repository.database.system.DictionaryRepository;
import com.dj.mingkong.portal.biz.service.system.DictionaryService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * 数据字典服务实现类
 *
 * @author zenglonghui
 * @date: 2023/4/14 17:48
 */
@Slf4j
@Service("dictionaryService")
public class DictionaryServiceImpl implements DictionaryService {
    @Resource
    private DictionaryRepository dictionaryRepository;
    @Resource
    private RedisTemplate<String, List<DictionaryVo>> redisTemplate;

    @Override
    public List<DictionaryVo> listTree() {
        final List<DictionaryEo> allEos = dictionaryRepository.lambdaQuery()
                .orderByAsc(DictionaryEo::getSort).list();
        if (CollectionUtils.isEmpty(allEos)) {
            return Collections.emptyList();
        }
        final List<DictionaryVo> allVos = SmartBeanCopy.copy(allEos, DictionaryVo.class);
        final List<DictionaryVo> vos = allVos.stream().filter(vo ->
                CommonConstants.DEFAULT_ID.equals(vo.getParentId())).toList();
        setChildren(vos, allVos);
        return vos;
    }

    @Override
    public List<DictionaryVo> getByParentKey(String key) {
        final String redisKey = RedisConstants.DICT_ALL.concat(key);
        final List<List<DictionaryVo>> range = redisTemplate.opsForList().range(redisKey, 0, 1);
        if (CollectionUtils.isNotEmpty(range)) {
            return range.get(0);
        }
        final Optional<DictionaryEo> eoOptional = dictionaryRepository.lambdaQuery()
                .eq(DictionaryEo::getDictKey, key).
                eq(DictionaryEo::getStatus, StatusEnum.VALID.getIntVal()).oneOpt();
        if (eoOptional.isEmpty()) {
            return Collections.emptyList();
        }
        final List<DictionaryEo> dictionaryEos = dictionaryRepository.lambdaQuery()
                .eq(DictionaryEo::getParentId, eoOptional.get().getId())
                .eq(DictionaryEo::getStatus, StatusEnum.VALID.getIntVal())
                .orderByAsc(DictionaryEo::getSort).list();
        final List<DictionaryVo> vos = SmartBeanCopy.copy(dictionaryEos, DictionaryVo.class);
        redisTemplate.opsForList().leftPush(redisKey, vos);
        return vos;
    }

    private void setChildren(List<DictionaryVo> vos, List<DictionaryVo> allVos) {
        vos.forEach(vo -> {
            final List<DictionaryVo> childrenList = allVos.stream().filter(item ->
                    vo.getId().equals(item.getParentId())).toList();
            vo.setChildren(childrenList);
            if (CollectionUtils.isNotEmpty(childrenList)) {
                setChildren(childrenList, allVos);
            }
        });
    }

}
